<?php namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Page;
use Illuminate\Http\Request;
use Auth;

class PageController extends Controller
{
    public function setPage(Request $request){
        if(Auth::guard('admin')->guest())
            return response()->json(['level'=>'error','message'=>'请先登录']);

        $title = $request->get('title');
        $link  = $request->get('link');
        $tag   = $request->get('tag');
        $content = $request->get('content');
        $id      = $request->get('id');

        /** @var Page $thatPage */
        $thatPage = Page::whereId($id)->first();

        if(!$thatPage){
            $thatPage = new Page();
        }

        if(!is_null($title))$thatPage->title = $title;
        if(!is_null($link) && $link != '')$thatPage->link = $link;
        if(!is_null($tag))$thatPage->tag = $tag;
        if(!is_null($content))$thatPage->content = $content;
        $thatPage->status = 1;

        $thatPage->save();

        return response()->json(['level'=>'success','message'=>'设置或更新完成','data'=>$thatPage]);

    }
}